#!/usr/bin/env node

// usage example:
//  ./tests/ui io-list-box
// the file must have a related
// smoke X.html and X.nightmare.js

/* globals global, process, require, __dirname */

"use strict";

const path = require("path");
const fs = require("fs");

function exitIfNotFound(file)
{
  if (!fs.existsSync(file))
  {
    console.error("file not found: " + path.relative(__dirname, file));
    process.exit(1);
  }
}

function runTests(tests)
{
  const test = tests.shift();
  if (test)
  {
    test(runTests.bind(null, tests), indexFile);
  }
}

if (process.argv.length < 3)
{
  console.error("the component name is missing");
  console.error("example: ./tests/ui io-list-box");
  process.exit(1);
}

const nightmareFile = path.join(__dirname, process.argv[2] + ".nightmare.js");
exitIfNotFound(nightmareFile);

const indexFile =
  path.join(__dirname, "..", "smoke", process.argv[2] + ".html");
exitIfNotFound(indexFile);

const {tests} = require(nightmareFile);

const runner = () =>
{
  const test = tests.shift();
  if (test)
    test(runner, "file://" + indexFile);
};

runner();
